//
// Created by TNJ on 2021/6/29.
//
#include "stack.h"
#include <stdlib.h>
#include <stdio.h>

void stack_init(_stack_t *s, int cap){
    if(s == NULL || cap <= 0){
        return;
    }

    queue_arr_init(&s->queue1, cap);
    queue_arr_init(&s->queue2, cap);
}

void stack_destroy(_stack_t *s){
    if(s == NULL){
        return;
    }
    queue_arr_destroy(&s->queue1);
    queue_arr_destroy(&s->queue2);
}

void stack_push(_stack_t *s, int data){
    if(s == NULL){
        return;
    }
    enqueue_arr(&s->queue1, data);
}

int stack_pop(_stack_t *s){
    if(s == NULL){
        return NAN;
    }
    if(queue_arr_is_empty(&s->queue1)){
        return NAN;
    }

    while(queue_arr_length(&s->queue1) > 1){
        enqueue_arr(&s->queue2, dequeue_arr(&s->queue1));
    }
    int data = dequeue_arr(&s->queue1);
    while(!queue_arr_is_empty(&s->queue2)){
        enqueue_arr(&s->queue1, dequeue_arr(&s->queue2));
    }
    return data;
}

int stack_pop2(_stack_t *s){
    if(s == NULL){
        return NAN;
    }
    if(queue_arr_is_empty(&s->queue1)){
        return NAN;
    }
    int length = queue_arr_length(&s->queue1) - 1;
    int data;
    while(length--){
        data = dequeue_arr(&s->queue1);
        enqueue_arr(&s->queue1, data);
    }
    return dequeue_arr(&s->queue1);
}

bool stack_is_empty(_stack_t *s){
    if(s == NULL){
        return true;
    }
    return queue_arr_is_empty(&s->queue1);
}

bool stack_is_full(_stack_t *s){
    if(s == NULL){
        return false;
    }
    return queue_arr_is_full(&s->queue1);
}
